package com.sduept.nwld.lbtz.math;

/**
 * 复数的运算
 */
public class ComplexNumberUtil {
	
	/**
	 * 复数加法
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static ComplexNumber addition(ComplexNumber c1,ComplexNumber c2) {
		Double realPart = add( c1.getRealPart() , c2.getRealPart());
		Double imaginaryPart = add( c1.getImaginaryPart() , c2.getImaginaryPart());
		ComplexNumber c = new ComplexNumber();
		c.setRealAndImaginary(realPart, imaginaryPart);
		return c;
	}
	/**
	 * 复数减法
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static ComplexNumber subtraction(ComplexNumber c1,ComplexNumber c2) {
		Double realPart = subtract( c1.getRealPart() , c2.getRealPart());
		Double imaginaryPart = subtract( c1.getImaginaryPart() , c2.getImaginaryPart());
		ComplexNumber c = new ComplexNumber();
		c.setRealAndImaginary(realPart, imaginaryPart);
		return c;
	}
	
	
	
	
	/**
	 * 转化为 实部，虚部
	 * @param m
	 * @param p
	 * @return
	 */
	public static Double[] convertToMP(Double r,Double i) {
		Double[] ret = new Double[2] ;
		if(r == null || i == null) {
			return ret;
		}
		ret[0] = Math.sqrt(r*r + i*i);
		ret[1] = Math.atan2(i,r);
		return ret;
	}
	/**
	 * 转化为实部虚部
	 * @param m
	 * @param p
	 * @return
	 */
	public static Double[] convertToRI(Double m,Double p) {
		Double[] ret = new Double[2] ;
		if(m == null || p == null) {
			return ret;
		}
		ret[0] = m*Math.cos(p);
		ret[1] = m*Math.sin(p);
		return ret;
	}
	
	private static Double add(Double d1,Double d2) {
		if(d1 == null || d2 == null) {
			return null;
		}else {
			return d1 + d2;
		}
	}
	private static Double subtract(Double d1,Double d2) {
		if(d1 == null || d2 == null) {
			return null;
		}else {
			return d1 - d2;
		}
	}
}
